package ac.zlf.springmybatis.admin.bjui.controller;

import ac.zlf.springmybatis.admin.bjui.constant.Uri;
import ac.zlf.springmybatis.admin.bjui.controller.common.BaseController;
import ac.zlf.springmybatis.admin.bjui.util.ExecuteResult;
import ac.zlf.springmybatis.base.entity.User;
import ac.zlf.springmybatis.core.mybatis.domain.Page;
import ac.zlf.springmybatis.core.mybatis.domain.PageRequest;
import ac.zlf.springmybatis.domain.AmenuBean;
import ac.zlf.springmybatis.service.AmenuService;
import ac.zlf.springmybatis.vo.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AmenuController extends BaseController {

    @Autowired
    private AmenuService amenuService;

    /**
     * 展示-分页
     * @param request           [必填] request对象
     * @param pageParams        [必填] 分页参数
     * @param amenuBean       [必填] 搜索条件
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_PAGE_SHOW)
    public ModelAndView showPage(HttpServletRequest request, PageParams pageParams, AmenuBean amenuBean){
        ModelAndView modelAndView = new ModelAndView();
        User user = this.getUser(request);
        //构建pageRequest
        PageRequest pageRequest = this.handlePageParams(pageParams);
        //查找
        Page<AmenuBean> entityList = amenuService.findBeanPage(pageRequest, amenuBean, user.getId());

        modelAndView.addObject("searchObj",amenuBean);
        modelAndView.setViewName("_a_menu/show");
        this.handlePageData(entityList, pageParams, request);
        return  modelAndView;
    }

    @RequestMapping(Uri._A_MENU_TREE_SHOW)
    public ModelAndView showTree(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("_a_menu/tree_show");
        User user = this.getUser(request);
        modelAndView.addObject("list",amenuService.findBeanAll(new AmenuBean(),user.getId()));
        return modelAndView;
    }

    /**
     * 展示-详情
     * @param request           [必填] request对象
     * @param id                [必填] ID
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_DETAIL_SHOW)
    public ModelAndView showDetail(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        AmenuBean amenuBean = amenuService.findBeanById(id, null);
        modelAndView.addObject("entity", amenuBean);
        modelAndView.setViewName("_a_menu/detail");
        return  modelAndView;
    }

    /**
     * 新增-页面
     * @param request           [必填] request对象
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_ADD_SHOW)
    public ModelAndView showAdd(HttpServletRequest request, AmenuBean amenuBean) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("entity", amenuBean);
        modelAndView.setViewName("_a_menu/add_update_show");
        return  modelAndView;
    }

    /**
     * 新增-提交
     * @param request           [必填] request对象
     * @param amenuBean       [必填] 包含新增字段信息的bean
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_ADD_SUBMIT)
    @ResponseBody
    public Object doAddSubmit(HttpServletRequest request, AmenuBean amenuBean){
        amenuService.doAdd(amenuBean, this.getUser(request));
        return ExecuteResult.jsonReturn(200, Uri._A_MENU_PAGE_SHOW);
    }

    /**
     * 编辑-页面
     * @param request           [必填] request对象
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_UPDATE_SHOW)
    public ModelAndView showUpdate(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        User user = this.getUser(request);
        AmenuBean amenuBean = amenuService.findBeanById(id, user.getId());
        modelAndView.addObject("entity", amenuBean);
        modelAndView.setViewName("_a_menu/add_update_show");
        return  modelAndView;
    }

    /**
     * 编辑-提交
     * @param request           [必填] request对象
     * @param amenuBean       [必填] 包含编辑信息的bean
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_UPDATE_SUBMIT)
    @ResponseBody
    public Object doUpdateSubmit(HttpServletRequest request, AmenuBean amenuBean){
        amenuService.doUpdate(amenuBean, this.getUser(request));
        return ExecuteResult.jsonReturn(200, Uri._A_MENU_PAGE_SHOW);
    }

    /**
     * 软删除-提交
     * @param request           [必填] request对象
     * @param id                [必填] ID
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_DEL_SUBMIT)
    @ResponseBody
    public Object doDelSubmit(HttpServletRequest request, Integer id){
        amenuService.doDel(id, this.getUser(request));
        return ExecuteResult.jsonReturn(200, null);
    }

    /**
     * 删除-提交
     * @param request           [必填] request对象
     * @param id                [必填] ID
     * @return
     */
    @RequestMapping(value = Uri._A_MENU_DELETE_SUBMIT)
    @ResponseBody
    public Object doDeleteSubmit(HttpServletRequest request, Integer id){
        amenuService.doDelete(id, this.getUser(request));
        return ExecuteResult.jsonReturn(200, null);
    }
}